package com.ff.xyh.service.post.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.common.entity.post.PostRoom;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.service.post.service.PostRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.Function;


/**
 * <p>
 * 帖子群聊表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@RestController
@RequestMapping("/post-service")
public class PostRoomController {

    @Autowired
    private PostRoomService postRoomService;

    @PostMapping("get-post-room-id")
    public ResultObject getPostRoomById(
            @RequestParam(name = "id") String id) {
        PostRoom postRoom = postRoomService.getById(id);
        return ResultObject.ok().data("data", postRoom);
    }

    @PostMapping("get-post-room-post-ids-type")
    public ResultObject getPostRoomsByPostIdsAndPostType(
            @RequestParam(name = "postIds") List<String> postIds,
            @RequestParam(name = "postType") int postType) {
        if (postType == 0)
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        QueryWrapper<PostRoom> wrapper = new QueryWrapper<>();
        if (postIds != null && !postIds.isEmpty()) {
            // SELECT id,post_id,post_type,user_id,is_deleted,gmt_created,gmt_modified
            // FROM post_room
            // WHERE is_deleted=0
            // AND post_id = ? OR post_id = ? OR post_id = ? AND ( post_type = ? )
            // 这样会导致 类型不对的也能被匹配到
            // SELECT id,post_id,post_type,user_id,is_deleted,gmt_created,gmt_modified
            // FROM post_room
            // WHERE is_deleted=0
            // AND post_type = ? AND ( post_id = ? OR post_id = ? OR post_id )
            // 这样导致后面postId限定无效
//            postIds.stream().forEach(postId -> wrapper.or().eq("post_id", postId));
//            wrapper.and(r -> r.eq("post_type", postType));

            wrapper.eq("post_type", postType)
                    .in("post_id", postIds);


//            wrapper.eq("post_type", postType);
//            wrapper.and(r -> {
//                r.eq("post_id", postIds.get(0));
//                for (int i = 1; i < postIds.size(); i++) {
//                    r.or().eq("post_id", postIds.get(i));
//                }
//                return r;
//            });

            List<PostRoom> postRooms = postRoomService.list(wrapper);
            if (postRooms == null || postRooms.isEmpty())
                return ResultObject.error().message(ResultMessage.NO_RESULT);
            else
                return ResultObject.ok().data("items", postRooms);
        } else {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        }
    }

    @PostMapping("get-post-room-post-id-type")
    public ResultObject getPostRoomWithPostIdAndType(
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") int postType) {
        QueryWrapper<PostRoom> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId)
                .eq("post_type", postType)
                .eq("is_deleted", 0);
        PostRoom postRoom = postRoomService.getOne(wrapper);
        return ResultObject.ok().data("data", postRoom);
    }
    @PostMapping("get-post-rooms-user-id")
    public ResultObject getPostRoomsByUserId(
            @RequestParam(name = "userId") String userId) {
        QueryWrapper<PostRoom> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("is_deleted", 0);
        List<PostRoom> postRoomList = postRoomService.list(wrapper);
        return ResultObject.ok().data("items", postRoomList);
    }

    @PostMapping("insert-post-room")
    public ResultObject insertPostRoom(
            @RequestBody PostRoom postRoom) {
        QueryWrapper<PostRoom> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postRoom.getPostId())
                .eq("post_type", postRoom.getPostType())
                .eq("is_deleted", 0);
        if (postRoomService.getOne(wrapper) == null) {
            if (postRoomService.save(postRoom)) {
                return ResultObject.ok().data("data", postRoom);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        } else {
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        }
    }

    @PostMapping("delete-post-room-id")
    public ResultObject deletePostRoomById(
            @RequestParam(name = "id") String id) {
        PostRoom data = postRoomService.getById(id);
        if (data != null) {
            if (postRoomService.removeById(id)) {
                return ResultObject.ok().data("data", data);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        } else {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        }
    }
    @PostMapping("delete-post-room-id-user")
    public ResultObject deletePostRoomByIdAndUserId(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "userId") String userId) {
        QueryWrapper<PostRoom> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id)
                .eq("user_id", userId)
                .eq("is_deleted", 0);
        PostRoom data = postRoomService.getOne(wrapper);
        if (data != null) {
            if (postRoomService.removeById(id)) {
                return ResultObject.ok().data("data", data);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        } else {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        }
    }
}

